package ifmo.group5512.timer;

/**
 * Created with IntelliJ IDEA.
 * User: katerina
 * Date: 01.11.12
 * Time: 0:40
 * To change this template use File | Settings | File Templates.
 */
public class CurrentManagerConfiguration {

    private int intervalIdx = 0;
    private IntervalType intervalType = IntervalType.Back;
    private boolean manualInput = true;
    private boolean stopWork = false;
    private boolean rememberToSwitchManualInputFlag = false;

    public static CurrentManagerConfiguration self = new CurrentManagerConfiguration();

    private CurrentManagerConfiguration() {
    }


    public synchronized void switchInputState() {
        if (getIntervalType() != IntervalType.InputData && !(stopWork)) {
            manualInput = !(manualInput);
        } else {
            rememberToSwitchManualInputFlag = true;
        }
    }

    public synchronized boolean isManualInput() {
        if (rememberToSwitchManualInputFlag) {
            return !(manualInput);
        }
        return manualInput;
    }

    public synchronized boolean nextInterval(Module module) {
        if (!(stopWork)) {
            if (getWorkingModule() == module) {
                return true;
            }
            return false;
        }

        stopWork = false;
        if (intervalType == IntervalType.Back && module == Module.ModelingModule) {
            intervalType = IntervalType.Modeling;
            return true;
        }

        if (intervalType == IntervalType.Modeling && ((module == Module.FrontOffice && manualInput) || (module == Module.ModelingModule && !(manualInput)))) {
            intervalType = IntervalType.InputData;
            return true;
        }

        if  (intervalType == IntervalType.InputData && module == Module.BackOffice) {
            intervalType = IntervalType.Back;
            ++intervalIdx;
            return true;
        }

        stopWork = true;
        return false;
    }

    public synchronized boolean stopWork(Module module) {
        if (getWorkingModule() == module && !(stopWork)) {
            stopWork = true;
            if (rememberToSwitchManualInputFlag) {
                rememberToSwitchManualInputFlag = false;
                manualInput = !(manualInput);
            }
            return true;
        }
        return false;
    }

    public synchronized int getIntervalIdx() {
        return intervalIdx;
    }

    public synchronized IntervalType getIntervalType() {
        return intervalType;
    }

    public synchronized Module getWorkingModule() {
        if (intervalType == IntervalType.Back) {
            return Module.BackOffice;
        }

        if ((intervalType == IntervalType.Modeling) || (intervalType == IntervalType.InputData && !(manualInput))) {
            return Module.ModelingModule;
        }

        return Module.FrontOffice;
    }

    public synchronized String toString() {
        String result = "Iteration id: " + intervalIdx + "; manual input - " + isManualInput() + "; current working service: ";

        if (intervalType == IntervalType.InputData) {
            if (isManualInput()) {
                return result + "Front Office";
            }
            return result + "Modeling - input data";
        }

        if (intervalType == IntervalType.Back) {
            return result + "Back Office";
        }
        return result + "Modeling - process optimization";
    }


}
